
import mixin from '@/service/mixin';
import Pagination from '@/components/pagination';
import { keyMap } from '@/config';

export default {
    name: 'coupon-manual',
    components: {
        Pagination,
    },
    mixins: [mixin],
    data() {
        const checkPhone = (rule, value, callback) => {
            const re = /^09[0-9]{9}$/;
            if ((!value && (!this.issueDialog.form.serialNumber || this.issueDialog.form.serialNumber === '')) || (value && !re.test(value))) {
                return callback(new Error(this.$t('verify.plsInputCorrectPhoneNum'))); // 请输入正确手机号
            }
            callback();
        };
        const checkCouponTime = (rule, value, callback) => {
            if (value && value <= Date.now()) {
                return callback(new Error(this.$t('operation.deadlineMustAfterNow'))); // 优惠券到期时间必须大于当前时间
            }
            callback();
        };
        const checkCouponAmount = (rule, value, callback) => {
            if (value && value > 10000) {
                return callback(new Error(this.$t('operation.couponAmountMaxTips'))); // 优惠券面额最大为10000
            }
            callback();
        };
        return {
            dialogTitle:'',
            keyMap,
            triggerNode:keyMap.triggerNode,
            aouponStatus:keyMap.aouponStatus,
            user: '',
            form: {
                discountCouponType: '', // 优惠券类型
                appName: '', // App名称
                triggerNode:'',//节点名称
                nodeStatus:'',//节点状态
                // discountAmount: '',  // 面额
                // url: '',
                // operationUser: '', // 操作员
                // serialNumber: '', // 编号
                // timeRange: [],
                // timeType: '', // 时间类型
            },
            rules: {},
            appList: [],
            showAddDialog: false, // 显示发送优惠券弹框
            issueDialog: {
                isVisible: false,
                type: '',
                form: {
                    // type:'',//优惠券名称
                    triggerNode:'',//节点名称
                    validityPeriod:'',//优惠券有效期
                    appName: '', // App名称
                    discountAmount:'',//优惠券面额
                    discountCouponType: '', // 优惠券类型
                    operationUser: this.$storage.get('user').username,
                },
                rules: {
                    discountCouponType:[
                        { required: true, message: this.$t('verify.plsInput'), trigger: 'blur' }, // 请选择
                    ],
                    type: [
                        { required: true, message: this.$t('verify.plsInput'), trigger: 'blur' }, // 请选择
                    ],
                    triggerNode: [
                        { required: true, message: this.$t('verify.plsChoose'), trigger: 'blur' }
                    ],
                    validityPeriod:[
                        { required: true, message: this.$t('common.plsInptCouponDay'), trigger: 'blur' }, // 优惠券有效期
                        { pattern: /^[1-9]\d*$/, message: this.$t('operation.plsInputRightDiscountDay'), trigger: 'blur' }, // 请输入正确的优惠券有效期额
                    ],
                    appName: [
                        { required: true, message: this.$t('verify.plsChoose'), trigger: 'change' }, // 请选择
                    ],
                    discountAmount: [
                        { required: true, message: this.$t('operation.plsInputDiscountAmount'), trigger: 'blur' }, // 请输入优惠券面额
                        { pattern: /^[1-9]\d*$/, message: this.$t('operation.plsInputCorrectDiscountAmount'), trigger: 'blur' }, // 请输入正确的优惠券面额
                        { validator: checkCouponAmount, trigger: 'blur' }, //
                    ],
                },
                isDisabledBtn: false,
            },
            uploadLoading: false,
            uploadLoadFailed: null,
            uploadLoadName: null,
        };
    },
    methods: {
        edit(row){
            this.issueDialog.form={
                discountCouponType:row.discountCouponType,//优惠券名称
                triggerNode:row.triggerNode.toString(),//节点名称
                validityPeriod:row.validityPeriod,//优惠券有效期
                appName: row.appName, // App名称
                discountAmount:row.discountAmount,//优惠券面额
                operationUser: this.$storage.get('user').username,
                id:row.id

            };
            this.dialogTitle = this.$t('common.edit')
            this.issueDialog.isVisible = true;
            this.issueDialog.type = 'edit';

        },
        switchChange(row){
            let params = {
                id:row.id,
                nodeStatus:row.nodeStatus == 0 ?1:0,
                operationUser: this.$storage.get('user').username,
            };
            this.$api.operation.updateStatus(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.getTableData()
                }
            })
        },
        // 获取列表数据
        getTableData(page = this.page) {
            const params = this.fillParams(page);
            if (params) {
                console.warn()
                this.$api.operation.searchAutoCouponList(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                        data.resultData.forEach(item => {
                            item.nodeStatysBolean = item.nodeStatus == 1
                        });
                        this.tableData = data.resultData;
                        this.setPage(data.page, this.page);
                    }
                    this.tableMaxHeight = document.body.clientHeight - 280;
                });
            }
        },
        // 搜索列表数据
        search() {
            this.$refs.form.validate(val => {
                if (val) {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            });
        },
        // 配置查询和导出参数
        fillParams(page = this.page) {
            const { discountCouponType, appName,triggerNode,nodeStatus } = this.form;
            const params = {
                discountCouponType, 
                appName,
                triggerNode,
                nodeStatus,
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                },
            };
            return params;
        },
        getAppList() {
            const params = {
                appListDomain: this.$storage.get('user').domain
            };
            this.$api.systemManage.getAppListdomain(params).then(res => {
                const { status, data, error } = res;
                if ('1' === status && error === '00000000' && data instanceof Array) {
                    this.appList = data;
                }
            });
        },
        // 发放优惠券
        issueCoupon() {
            this.uploadLoadFailed = '';
            this.uploadLoadName = '';
            this.dialogTitle = this.$t('common.addCoupon')
            this.issueDialog.isVisible = true;
            this.$nextTick(() => {
                this.$refs['issueDialog.form'].resetFields();
            });
        },
        // 确认发放优惠券
        submint() {
            this.$refs['issueDialog.form'].validate((valid) => {
                if (valid) {
                    const form = this.issueDialog.form;
                    this.$api.operation.ticketAuto(form).then(res=>{
                        const { status, data, error } = res;
                        if ('1' === status && error === '00000000' ) {
                            this.issueCouponClose();
                            this.getTableData()
                        }
                    
                    })
                    console.warn(form)
                }
            })
        },
        // 关闭发放优惠券
        issueCouponClose() {
            this.issueDialog.form = {
                triggerNode:'',//节点名称
                validityPeriod:'',//优惠券有效期
                appName: '', // App名称
                discountAmount:'',//优惠券面额
                discountCouponType: '', // 优惠券类型
                operationUser: this.$storage.get('user').username,
            };
            this.$refs['issueDialog.form'].resetFields();
            this.issueDialog.isDisabledBtn = false;
            this.issueDialog.isVisible = false;
        },
    },
    filters: {
    },
    created() {
        this.user = this.$storage.get('user');
        this.getTableData({currentPage: this.page.currentPage}, this.form);
        this.getAppList();
    },
}
